/**
 * @(#)ProcessContainer.java          Created on 2003/10/27
 *
 * Copyright (c) 2000-2003 IBTechnology, Inc.
 * All rights reserved.
 * 
 * This software is the confidential and proprietary information of
 * IBTechnology, Inc. ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with IBTechnology.
 */
package libnetdb.dm.assoc.dhp.demo;
import java.util.*;
import java.text.*;
import java.io.*;

import junit.framework.TestCase;
import libnetdb.dm.assoc.base.*;
import libnetdb.dm.assoc.dhp.*;
import libnetdb.dm.assoc.util.*;

/**
 * @author Lethe Huang
 * @version 1.000, 2003/10/27
 * @see 
 * @since J2SDK1.4.1, J2SDKEE1.3.1
 */
public class ProcessInformation extends TestCase{
	private static ArrayList indexList = new ArrayList();
	   {
		   IndexManager manager = IndexManager.getInstance();
		   Iterator it = manager.builderIterator();
		   while(it.hasNext())
			   indexList.add(((IndexBuilder)it.next()).createIndex());        
	   }

	   private static void printUsage(PrintStream ps){
		   ps.println("usage: filename support confidence");
	   }
	public static void listRules(Iterator ruleIterator){
			DecimalFormat format = new DecimalFormat("#0.000");
			while(ruleIterator.hasNext()){
				Rule rule = (Rule) ruleIterator.next();
				System.out.print(rule.toString());
				for(int i=0;i<indexList.size();++i){
					RuleIndex index = (RuleIndex) indexList.get(i);
					System.out.print(", " + 
							format.format(index.getIndex(rule)));
				}
				System.out.println();                   
			}        
		}
    
    private void implement(String[] args){
		
			   double first = Runtime.getRuntime().freeMemory();
			 
			   double support = Double.parseDouble(args[0]);
			   double confidence = Double.parseDouble(args[1]);
        
			   NameSpace ns = new BasicNameSpace();
			
			   TextInput input = new TextInput(args[2], ns);
		
			   DirectBuilder output = new DirectBuilder();
			   DHPMiner miner = new DHPMiner(input, output, support);
			   miner.addProgressListener(new ConsoleMessenger());
			   miner.start();
			   try{
				miner.join();
			   }catch(Exception e){
			   	 
			   }
			   
			   RuleSet rs = output.getRuleSet();
			   rs.setNameSpace(ns);
        
			   Iterator it = rs.ancensterIterator(new Itemset(new int[0]));
			   while(it.hasNext()){
				   Litemset litemset = (Litemset) it.next();
				   System.out.println(
						   rs.toString(litemset) + ": " + litemset.count);
			   }

			   System.out.println("All Rules: ");
			   listRules(rs.getRules(support, confidence));
			   double last = Runtime.getRuntime().freeMemory();
		
        
	
    }
    
    public void testMemoryInfomation(String[]args){
		double first = Runtime.getRuntime().freeMemory();
		implement(args);
		System.out.println( "Total usage memory :" + ( Runtime.getRuntime().freeMemory() - first ) );
    }
   
    
	public static void main(String[] args) {
		junit.swingui.TestRunner.run( ProcessInformation.class );
	}
}
